1 module std.file; 2 import core.stdc.stdio; 3 4 version(Windows) 5 { 6 import core.sys.windows.windef; 7 extern(Windows) nothrow @nogc DWORD GetCurrentDirectoryA(DWORD, LPSTR); 8 } 9 10 string getcwd() 11 { 12 version(Windows) 13 { 14 char[4096] buff = void; 15 immutable n = GetCurrentDirectoryA(cast(DWORD)buff.length, buff.ptr); 16 char[] ret = new char[n]; 17 ret[] = buff[0..n]; 18 return cast(string)ret; 19 } 20 else return ""; 21 } 22 23 bool exists(string file) 24 { 25 auto fHandle = fopen((file~'\0').ptr, "r"); 26 if(fHandle != null) 27 { 28 fclose(fHandle); 29 return true; 30 } 31 return false; 32 } 33 34 ubyte[] read(string file) 35 { 36 auto fHandle = fopen((file~'\0').ptr, "rb"); 37 if(fHandle != null) 38 { 39 ubyte[] ret; 40 fseek(fHandle, 0, SEEK_END); 41 auto sz = ftell(fHandle); 42 scope(exit) fclose(fHandle); 43 if(sz > 0) 44 { 45 fseek(fHandle, 0, SEEK_SET); 46 size_t fileSize = cast(size_t)sz; 47 ret = new ubyte[fileSize]; 48 auto readSize = fread(cast(void*)ret.ptr, fileSize, 1, fHandle); 49 return ret[0..readSize]; 50 } 51 } 52 return []; 53 } 54 55 56 void write(string path, ubyte[] buffer) 57 { 58 auto fHandle = fopen((path~"\0").ptr, "w"); 59 if(fHandle != null) 60 { 61 scope(exit) fclose(fHandle); 62 foreach(b; buffer) 63 if(fputc(cast(int)b, fHandle) == EOF) return; 64 } 65 } 66 void write(string path, void[] buffer){write(path, cast(ubyte[])buffer);} 67 void write(string path, string buffer){write(path, cast(ubyte[])buffer);} 68 69 void remove(string filename) 70 { 71 if(!core.stdc.stdio.remove((filename~"\0").ptr)) 72 { 73 assert(false, "Could not remove "~filename); 74 } 75 } 76 77 bool isDir(string path) 78 { 79 return path[$-1] == '/' || path[$-1] == '\\'; 80 }